package com.jwthhealth_pub.old.sportfitness.view;

import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jwthhealth_pub.R;
import com.jwthhealth_pub.common.view.TitleLayout;
import com.jwthhealth_pub.old.common.Constant;
import com.jwthhealth_pub.old.sportfitness.module.SprotCourseInfoModule;
import com.jwthhealth_pub.old.sportfitness.presenter.ISportCourseInfoListPresenter;
import com.jwthhealth_pub.old.sportfitness.presenter.ISportCourseInfoListPresenterCompl;
import com.jwthhealth_pub.old.sportfitness.view.adapter.SportItemBodyAdatper;
import com.jwthhealth_pub.old.sportfitness.view.adapter.SportItemFootHorAdapter;
import com.jwthhealth_pub.old.sportfitness.view.adapter.SportItemFootVerAdapter;

import java.io.IOException;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/1/6.
 */
public class SportCourseListActivity extends Activity {

    @BindView(R.id.btn_left)
    ImageView btnLeft;
    @BindView(R.id.btn_right)
    ImageView btnRight;
    @BindView(R.id.tv_index)
    TextView tvIndex;
    @BindView(R.id.bottom_layout)
    RelativeLayout bottomLayout;
    @BindView(R.id.head_surfaceview)
    SurfaceView headSurfaceview;
    @BindView(R.id.head_title)
    TextView headTitle;
    @BindView(R.id.body_item_rv)
    RecyclerView bodyItemRv;
    @BindView(R.id.foot_describe)
    TextView footDescribe;
    @BindView(R.id.foot_hor_rv)
    RecyclerView footHorRv;
    @BindView(R.id.foot_ver_rv)
    RecyclerView footVerRv;
    @BindView(R.id.sign_topbar)
    TitleLayout signTopbar;
    private SprotCourseInfoModule.DataBean data;
    private ISportCourseInfoListPresenter iSportCourseInfoListPresenter;
    private List<SprotCourseInfoModule.DataBean.VideoListBean> list;
    private int max;
    private int index;
    private SportItemFootVerAdapter footVerAdapter;
    private SportItemFootHorAdapter footHorAdapter;
    private SportItemBodyAdatper bodyAdapter;

    SprotCourseInfoModule.DataBean.VideoListBean mAdapterData;
    private List<String> footHorData;
    private List<String> foorVerData;
    private MediaPlayer mMediaPlayer;

    public int getMax() {
        return max;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sport_course_list);
        ButterKnife.bind(this);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        }


        Bundle bundle = getIntent().getExtras();
        data = (SprotCourseInfoModule.DataBean) bundle.getSerializable(Constant.SPORT_DATA);
        list = data.getVideo_list();
        iSportCourseInfoListPresenter = new ISportCourseInfoListPresenterCompl(this, list);
        index = (int) bundle.getSerializable(Constant.SPORT_INDEX);
        tvIndex.setText((index + 1) + "/" + list.size());

        initTop();
        mMediaPlayer = new MediaPlayer();
        RenderingUI(list.get(index), false);
    }

    /**
     * 初始化ui
     *
     * @param videoListBean 数据
     */
    private void RenderingUI(SprotCourseInfoModule.DataBean.VideoListBean videoListBean, boolean isRefresh) {

        mAdapterData = videoListBean;

        headTitle.setText(mAdapterData.getName());

        bodyAdapter = new SportItemBodyAdatper(this, mAdapterData);
        bodyItemRv.setLayoutManager(new LinearLayoutManager(this));
        bodyItemRv.setAdapter(bodyAdapter);

        footHorData = mAdapterData.getThumb_list();
        footHorAdapter = new SportItemFootHorAdapter(this, footHorData);
        LinearLayoutManager layout = new LinearLayoutManager(this);
        layout.setOrientation(LinearLayoutManager.HORIZONTAL);
        footHorRv.setLayoutManager(layout);
        footHorRv.setAdapter(footHorAdapter);

        foorVerData = mAdapterData.getSport_desc();
        footVerAdapter = new SportItemFootVerAdapter(this, foorVerData);
        footVerRv.setLayoutManager(new LinearLayoutManager(this));
        footVerRv.setAdapter(footVerAdapter);

        initVideo(videoListBean.getVideo_path(), isRefresh);
    }

    /**
     * 加载视频
     *
     * @param video_path
     */
    private void initVideo(final String video_path, boolean isRefresh) {

        if (isRefresh) {
            try {
                mMediaPlayer.stop();
                mMediaPlayer.reset();
//                mMediaPlayer = new MediaPlayer();
                mMediaPlayer.setDataSource(video_path);
                mMediaPlayer.setLooping(true);//循环播放
                mMediaPlayer.prepareAsync();
                mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                    @Override
                    public void onPrepared(MediaPlayer mediaPlayer) {
                        mMediaPlayer.start();
                    }
                });
            } catch (IOException e) {
                Log.d("SportCourseListActivity", e.toString());
            }
        } else {
            SurfaceHolder surfaceHolder = headSurfaceview.getHolder();
            surfaceHolder.addCallback(new SurfaceHolder.Callback() {
                @Override
                public void surfaceCreated(SurfaceHolder surfaceHolder) {

                    mMediaPlayer.setDisplay(surfaceHolder);
                    Log.d("SportCourseListActivity", video_path);
                    try {
                        mMediaPlayer.setDataSource(video_path);
                        mMediaPlayer.setLooping(true);//循环播放
                        mMediaPlayer.prepareAsync();
                        mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                            @Override
                            public void onPrepared(MediaPlayer mediaPlayer) {
                                mMediaPlayer.start();
                            }
                        });
                    } catch (IOException e) {
                        Log.d("SportCourseListActivity", e.toString());
                    }
                }

                @Override
                public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {

                }

                @Override
                public void surfaceDestroyed(SurfaceHolder surfaceHolder) {

                }
            });
        }

    }

    private void ResreshUI(SprotCourseInfoModule.DataBean.VideoListBean videoListBean) {
        mAdapterData = videoListBean;
        footHorData = mAdapterData.getThumb_list();
        foorVerData = mAdapterData.getSport_desc();

        headTitle.setText(mAdapterData.getName());

        bodyAdapter.notifyDataSetChanged();
        footHorAdapter.notifyDataSetChanged();
        footVerAdapter.notifyDataSetChanged();
    }

    private void initTop() {

        signTopbar.setTitle("运动健身 " + data.getName());

        signTopbar.setOnTitleLayoutListener(new TitleLayout.TitleLayoutListener() {
            @Override
            public void leftclick() {
                finish();
            }

            @Override
            public void leftTextClick() {

            }

            @Override
            public void rightclick() {

            }

            @Override
            public void rightTextClick() {

            }
        });
    }

    @OnClick({R.id.btn_left, R.id.btn_right})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_left:
                index = iSportCourseInfoListPresenter.left(index);
                Log.d("SportCourseListActivity", "index:" + index);
                RenderingUI(list.get(index), true);
                tvIndex.setText((index + 1) + "/" + list.size());
                break;
            case R.id.btn_right:
                index = iSportCourseInfoListPresenter.right(index);
                Log.d("SportCourseListActivity", "index:" + index);
                RenderingUI(list.get(index), true);
                tvIndex.setText((index + 1) + "/" + list.size());
                break;
        }
    }
}
